﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WiTeach.ClassManagement.DAL;
using WiTeach.ClassManagement.Model;
using WiTeach.Tools;

namespace WiTeach.JobManagement.View
{
    /// <summary>
    /// JobList.xaml 的交互逻辑
    /// </summary>
    public partial class JobList : Page
    {
        public int classHourID;
        public int lessonID;
        public JobList()
        {
            InitializeComponent();
            // 修改页面背景主题
            ImageBrush brush = new ImageBrush();
            brush.Stretch = Stretch.Fill;
            brush.ImageSource = new BitmapImage(new Uri(App.backgroundImage, UriKind.RelativeOrAbsolute));
            this.Background = brush;
        }

        public JobList(string className, int LessonID, int ClassHour_ID)
        {
            InitializeComponent();
            // 修改页面背景主题
            ImageBrush brush = new ImageBrush();
            brush.Stretch = Stretch.Fill;
            brush.ImageSource = new BitmapImage(new Uri(App.backgroundImage, UriKind.RelativeOrAbsolute));
            this.Background = brush;
            this.titleNameLabel.Content = className;
            this.classHourID = ClassHour_ID;
            this.lessonID = LessonID;

        }

        private void pdfOpen()
        {

            List<ClassHour> list = ClassHourDAL.getClassListByID(this.lessonID, this.classHourID);
            string pdfPath = list[0].Class_Job_Url.ToString();
            Process process = new Process();
            try
            {
                process.StartInfo.FileName = pdfPath;
                process.StartInfo.WindowStyle = ProcessWindowStyle.Maximized;
                process.Start();
                process.WaitForExit();
                process.Close();
                GC.Collect();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
            finally
            {
                process.Close();
                GC.Collect();
            }

        }

        private void joblist_backmain_mouse_enter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Image image = sender as Image;
            image.Height = 66;
            image.Width = 104;
        }
        private void joblist_backmain_mouse_leave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Image image = sender as Image;
            image.Height = 60;
            image.Width = 98;
        }
        private void joblist_back_mouse_enter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Image image = sender as Image;
            image.Height = 66;
            image.Width = 104;
        }
        private void joblist_back_mouse_leave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Image image = sender as Image;
            image.Height = 60;
            image.Width = 98;
        }

        private void backHomeButton_Click(object sender, RoutedEventArgs e)
        {
            NavigationService.GetNavigationService(this).Navigate(new Uri("/WiTeach;component/CourseManagement/View/CourseIndex.xaml", UriKind.Relative));
        }

        private void backPreLevel(object sender, RoutedEventArgs e)
        {
            this.NavigationService.GoBack();
            //NavigationService.GetNavigationService(this).Navigate(new Uri("/WiTeach;component/ClassManagement/View/ClassIndex.xaml", UriKind.Relative));
        }

        private void experimentMaterialButton(object sender, RoutedEventArgs e)
        {
            NavigationService.GetNavigationService(this).Navigate(new Uri("/WiTeach;component/MaterialManagement/View/materialShow.xaml", UriKind.Relative));
        }

        private void contentExport(object sender, RoutedEventArgs e)
        {
            String sql1 = "SELECT name as  Lesson_Name FROM ta_course WHERE id = " + lessonID;
            DataTable lessonName = SqlHelper.ExecuteDataTable(sql1);
            String lesson_name = lessonName.Rows[0]["Lesson_Name"].ToString();

            String sql2 = "SELECT name as  ClassHour_Name FROM ta_lesson WHERE id = " + classHourID;

            DataTable className = SqlHelper.ExecuteDataTable(sql2);
            String class_name = className.Rows[0]["ClassHour_Name"].ToString();
            FileHelper fp = new FileHelper();

            string content = jobContent.Text;
            string exercise = lesson_name + "-" + class_name;
            var ts = DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, 0);
            string create_time = Convert.ToInt64(ts.TotalSeconds).ToString();
            string uname = App.userName;
            string sqls = "INSERT INTO ta_exercise (uname,content,exercise,create_time) VALUES('" + uname + "','" + content + "','" + exercise + "','" + create_time + "')";
            SqlHelper.ExecuteNonQuery(sqls);
            System.Windows.Forms.MessageBox.Show("提交成功！");

        }




        private void contentExport22(object sender, RoutedEventArgs e)
        {
            String sql1 = "SELECT name as  Lesson_Name FROM ta_course WHERE id = " + lessonID;
            DataTable lessonName = SqlHelper.ExecuteDataTable(sql1);
            String lesson_name = lessonName.Rows[0]["Lesson_Name"].ToString();

            String sql2 = "SELECT name as  ClassHour_Name FROM ta_lesson WHERE id = " + classHourID;

            DataTable className = SqlHelper.ExecuteDataTable(sql2);
            String class_name = className.Rows[0]["ClassHour_Name"].ToString();
            FileHelper fp = new FileHelper();

            String fullPath = fp.GetExecutionDirectoryFileFullPath("jobContent\\", lesson_name + "-" + class_name + ".txt");

            if (!File.Exists(fullPath))
            {
                File.Create(fullPath);

                FileStream fs1 = new FileStream(fullPath, FileMode.Create, FileAccess.Write);//创建写入文件 
                StreamWriter sw = new StreamWriter(fs1);
                sw.WriteLine(jobContent.Text);//开始写入值
                sw.Close();
                fs1.Close();
                System.Windows.Forms.MessageBox.Show("导出成功！");
            }
            else
            {
                FileStream fs = new FileStream(fullPath, FileMode.Create);
                StreamWriter sw = new StreamWriter(fs);
                sw.Write(jobContent.Text);
                sw.Close();
                fs.Close();
                System.Windows.Forms.MessageBox.Show("导出成功！");
            }

            //string file_save = System.Configuration.ConfigurationManager.AppSettings["file_save"];
            //FileStream fs = new FileStream(@file_save, FileMode.Create);
            //StreamWriter sw = new StreamWriter(fs);
            //sw.Write(jobContent.Text);
            //sw.Close();
            //fs.Close();
            //System.Windows.Forms.MessageBox.Show("导出成功！请在相应txt文件查看（默认为jobContent.txt）");
        }

        private void uv4_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                string KEIL_Lic_path = System.Configuration.ConfigurationManager.AppSettings["app_KEIL_Lic"];
                Process uv4 = new Process();
                uv4.StartInfo.FileName = KEIL_Lic_path;
                uv4.Start();
                uv4.WaitForExit();
                uv4.Close();
            }
            catch (Exception)
            {
            }

            //   System.Diagnostics.Process.Start(@KEIL_Lic_path);
        }

        private void progisp_Click(object sender, RoutedEventArgs e)
        {
            string progisp_path = System.Configuration.ConfigurationManager.AppSettings["app_progisp"];


            //     System.Diagnostics.Process.Start(@progisp_path);
            try
            {
                Process progisp = new Process();
                progisp.StartInfo.FileName = progisp_path;
                progisp.Start();
                progisp.WaitForExit();
                progisp.Close();
                GC.Collect();
            }
            catch (Exception)
            {
            }

        }

        private void flymcu_Click(object sender, RoutedEventArgs e)
        {
            string flymcu_path = System.Configuration.ConfigurationManager.AppSettings["app_flymcu"];
            //          System.Diagnostics.Process.Start(@flymcu_path);
            try
            {
                Process flymcu = new Process();
                flymcu.StartInfo.FileName = flymcu_path;
                flymcu.Start();
                flymcu.WaitForExit();
                flymcu.Close();
                GC.Collect();
            }
            catch (Exception)
            {
            }

        }

        private void arduino_Click(object sender, RoutedEventArgs e)
        {
            string arduino_path = System.Configuration.ConfigurationManager.AppSettings["app_arduino"];
            //        System.Diagnostics.Process.Start(@arduino_path);
            try
            {
                Process arduino = new Process();
                arduino.StartInfo.FileName = arduino_path;
                arduino.Start();
                arduino.WaitForExit();
                arduino.Close();
                GC.Collect();
            }
            catch (Exception)
            {
            }

        }

        private void sa_Click(object sender, RoutedEventArgs e)
        {
            string sa_path = System.Configuration.ConfigurationManager.AppSettings["app_S4A15"];
            Process sa = new Process();
            try
            {
                sa.StartInfo.FileName = sa_path;
                Console.WriteLine("sa_path" + sa_path);
                sa.Start();
                sa.WaitForExit();
                sa.Close();
                GC.Collect();
            }
            catch (Exception ex)
            {
                Console.WriteLine(ex);
            }
        }

        private void course_logout_mouse_enter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Image image = sender as Image;
            image.Height = 35;
            image.Width = 74;
        }
        private void course_logout_mouse_leave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Image image = sender as Image;
            image.Height = 28;
            image.Width = 68;
        }
        private void course_keil_mouse_enter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Image image = sender as Image;
            image.Height = 56;
            image.Width = 59;
        }
        private void course_keil_mouse_leave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Image image = sender as Image;
            image.Height = 46;
            image.Width = 49;
        }
        private void course_arduino_mouse_enter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Image image = sender as Image;
            image.Height = 54;
            image.Width = 60;
        }
        private void course_arduino_mouse_leave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Image image = sender as Image;
            image.Height = 48;
            image.Width = 54;
        }
        private void course_flymcu_mouse_enter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Image image = sender as Image;
            image.Height = 52;
            image.Width = 60;
        }
        private void course_flymcu_mouse_leave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Image image = sender as Image;
            image.Height = 48;
            image.Width = 54;
        }
        private void course_progisp_mouse_enter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Image image = sender as Image;
            image.Height = 52;
            image.Width = 59;
        }
        private void course_progisp_mouse_leave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Image image = sender as Image;
            image.Height = 48;
            image.Width = 54;
        }
        private void course_scatch_mouse_enter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Image image = sender as Image;
            image.Height = 53;
            image.Width = 59;
        }
        private void course_scatch_mouse_leave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Image image = sender as Image;
            image.Height = 48;
            image.Width = 54;
        }

        private void joblist_taskexport_mouse_enter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Image image = sender as Image;
            image.Height = 55;
            image.Width = 55;
        }
        private void joblist_taskexport_mouse_leave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            Image image = sender as Image;
            image.Height = 48;
            image.Width = 68;
        }

        private void openpaper(object sender, RoutedEventArgs e)
        {
            List<ClassHour> list = ClassHourDAL.getClassListByID(this.lessonID, this.classHourID);
            // this.jobImage.DataContext = list;
            this.pdfOpen();
        }

    }
}
